package us.versus.them.kitkattoe;

import flash.display.Graphics;
import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.geom.Point;
import flash.media.Sound;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.geom.Rectangle;

import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;

class Square extends Sprite {
	var bitmap:Bitmap;
	var data:BitmapData;
	var src:BitmapData;

	public function new( ?data:BitmapData ) {
		super();
		if ( null != data ) {
			this.data = data;
			this.data.fillRect( 
				new Rectangle( 0,0,this.data.width,this.data.height )
				, 0x0F 
			);
			this.bitmap = new Bitmap( this.data );
			this.addChild( this.bitmap );
		}
		this.src = null;
	}

	public function rect() {
		return new Rectangle( 0,0,this.data.width,this.data.height );
	}

	public function copy() : Square {
		var square = new Square();
		square.src = this.src;
		return square;
	}

	public function take( sourceBitmapData : BitmapData ) {
		this.src = sourceBitmapData;
		if ( null != this.data ) {
			this.data.fillRect( rect(), 0xFFFFFF );
			this.data.copyPixels(
				sourceBitmapData
				, new Rectangle( 0,0, sourceBitmapData.width,sourceBitmapData.height )
				, new Point( 0, 0 )
			);
		}
	}

	public function taken() : Bool {
		return null != this.src;
	}

	public function owns( player : BitmapData ) : Bool {
		return player == this.src;
	}

	public function holds() : BitmapData {
		return src;
	}
}
